package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IEmployOrganizationDAO;
import org.jsola.hr.dto.EmployOrganizationAddDTO;
import org.jsola.hr.dto.EmployOrganizationUpdateDTO;
import org.jsola.hr.entity.EmployOrganizationDO;
import org.jsola.hr.query.EmployOrganizationQuery;
import org.jsola.hr.service.IEmployOrganizationService;
import org.jsola.hr.vo.EmployOrganizationListVO;
import org.jsola.hr.vo.EmployOrganizationVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卫人云-用人机构
 *
 * @author zhr
 */
@Service("hrEmployOrganizationServiceImpl")
public class EmployOrganizationServiceImpl implements IEmployOrganizationService {

    @Autowired
    private IEmployOrganizationDAO employOrganizationDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public EmployOrganizationVO save(EmployOrganizationAddDTO employOrganizationAddDTO, TokenUser tokenUser) {
        // 转DO
        EmployOrganizationDO employOrganizationDO = employOrganizationAddDTO.to(EmployOrganizationDO.class);
        // 保存
        employOrganizationDO = save(employOrganizationDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return employOrganizationDO.to(EmployOrganizationVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(EmployOrganizationUpdateDTO employOrganizationUpdateDTO, TokenUser tokenUser) {
        //转DO
        EmployOrganizationDO employOrganizationDO = employOrganizationUpdateDTO.to(EmployOrganizationDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(employOrganizationDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... employOrganizationIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) employOrganizationIds);
    }


    @Override
    public EmployOrganizationVO selectById(String employOrganizationId, String siteId) {
        EmployOrganizationDO employOrganizationDO = selectDOById(employOrganizationId, siteId);
        if (employOrganizationDO == null) {
            return null;
        }
        return employOrganizationDO.to(EmployOrganizationVO.class);
    }

    @Override
    public List<EmployOrganizationListVO> select(EmployOrganizationQuery employOrganizationQuery, String siteId) {
        List<EmployOrganizationDO> employOrganizationDOList = selectDO(employOrganizationQuery, siteId);
        if(CollectionUtils.isEmpty(employOrganizationDOList)) {
            return employOrganizationDOList == null ? null : new ArrayList<>();
        }
        return employOrganizationDOList.stream()
                .map(employOrganizationDO -> employOrganizationDO.to(EmployOrganizationListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(EmployOrganizationQuery employOrganizationQuery, String siteId) {
        Example example = buildExample(employOrganizationQuery,siteId);
        return employOrganizationDAO.selectCountByExample(example);
    }

    @Override
    public Page<EmployOrganizationListVO> selectPage(EmployOrganizationQuery employOrganizationQuery, String siteId) {
        Example example = buildExample(employOrganizationQuery,siteId);
        Page<EmployOrganizationDO> page = employOrganizationDAO.selectPageByExample(example,
                employOrganizationQuery.getPageNo(),
                employOrganizationQuery.getPageSize());

        return page.to(EmployOrganizationListVO.class);
    }

    @Override
    public EmployOrganizationDO selectDOById(String employOrganizationId, String siteId) {
        return listById(employOrganizationId, siteId);
    }

    @Override
    public List<EmployOrganizationDO> selectDO(EmployOrganizationQuery employOrganizationQuery, String siteId) {
        Example example = buildExample(employOrganizationQuery,siteId);
        return employOrganizationDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param employOrganizationQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(EmployOrganizationQuery employOrganizationQuery, String siteId) {
        Example example = new Example(EmployOrganizationDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,employOrganizationQuery.getOrders());
        return example;
    }
}




